﻿using NX5Web.Entities;
using NX5Web.Filters;
using NX5Web.Utils;
using CommHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.AccessControl;
using System.Web.Http;
using System.Web.Http.Results;
using System.Configuration;

namespace NX5Web.Controllers
{
    public class CommonDate : ApiBase
    {
        public string Action { get; set; }

        public string Parameters { get; set; } 

        public int OpUid { set; get; }
    }

    //[ApiAuthorize]
    public class DataManagerController : BaseApiController
    { 
        [HttpPost]
        public IHttpActionResult Command(CommonDate CD)
        {
            try
            { 
                DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), CD.OpUid));

                DataToInfo dataInfo;
                try
                {
                    dataInfo = dm.Procedure(CD.Action, CD.Parameters);
                }
                catch (Exception ex)
                {
                    ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.ToString());
                    throw new Exception(ex.ToString());
                }

                return ReturnSuccess(dataInfo.table);
            }
            catch(Exception ex)
            {
                DataManager dm = new DataManager();
                Dictionary<String, String> dic = new Dictionary<string, string>();
                dic.Add("Command", CD.Action);
                dic.Add("Parameters", CD.Parameters);
                dm.AddError(dic);
                return ReturnFail(null);
            }
        }


        [HttpPost]
        public IHttpActionResult CommandAddSet(CommonDate CD)
        {
            try
            { 
                DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), CD.OpUid));
                List<DataToList> res_list = new List<DataToList>();
                try
                {
                    List<Dictionary<string, string>> JSONList = null;
                    JSONList = JSONManager.ListJsonParse(CD.Parameters);
                    for (int i = 0; i < JSONList.Count; i++)
                    {
                        var jsonParams = "[" + Newtonsoft.Json.JsonConvert.SerializeObject(JSONList[i]) + "]";
                        res_list.Add(dm.ProcedureSet(CD.Action, jsonParams));
                    }

                }
                catch (Exception ex)
                {

                    ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
                    throw new Exception(ex.ToString());
                }

                return ReturnSuccess(res_list);
            }
            catch (Exception ex)
            {
                DataManager dm = new DataManager();
                Dictionary<String, String> dic = new Dictionary<string, string>();
                dic.Add("Command", CD.Action);
                dic.Add("Parameters", CD.Parameters);
                dm.AddError(dic);
                return ReturnFail(null);
            }
        }


        [HttpPost]
        public IHttpActionResult CommandSet(CommonDate CD)
        {
            try
            { 
                DataManager dm = new DataManager(new DataLog(Url.Request.RequestUri.ToString(), CD.OpUid));
                DataToList dataList;
                try
                {
                    dataList = dm.ProcedureSet(CD.Action, CD.Parameters);

                    for (int i = 0; i < dataList.tables.Count; i++)
                    {
                        if (dataList.tables[i].table.Columns.Contains("PassWord"))
                        {
                            dataList.tables[i].table.Columns.Remove("PassWord");
                        }
                    }
                }
                catch (Exception ex)
                {
                    //String Parms = "{\"Name\":\"" + CD.Name + "\",\"Parameters\":\"" + CD.Parameters + "\",\"AuthCode\":\"" + CD.AuthCode + "\"}"; 
                    ErrorHandle.SaveError(Url.Request.RequestUri.ToString(), Newtonsoft.Json.JsonConvert.SerializeObject(CD), ex.Message);
                    throw new Exception(ex.ToString());
                }

                return ReturnSuccess(dataList);
            }
            catch (Exception ex)
            {
                DataManager dm = new DataManager();
                Dictionary<String, String> dic = new Dictionary<string, string>();
                dic.Add("Command", CD.Action);
                dic.Add("Parameters", CD.Parameters);
                dm.AddError(dic);
                return ReturnFail(null);
            }
        }
    }
}
